package presentation.entities;

import java.util.List;
import java.util.Map;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ListSelectionListenerWrapper implements ListSelectionListener {

    private JList sourceJList = null;
    private JList targetJList = null;
    private Map<String, List<String>> dataMap = null;

    public ListSelectionListenerWrapper(JList source, JList target, Map<String, List<String>> map) {
        this.sourceJList = source;
        this.targetJList = target;
        this.dataMap = map;
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        Object sourceEvent = e.getSource();

        //Use the selcted value in sourceJList as 'key' and bind the list dataMap.get('key') to targetJList.
        if (sourceEvent.equals(sourceJList)) {
            List<String> selectionList = sourceJList.getSelectedValuesList();
            if (selectionList == null || selectionList.size() != 1) {
                return;
            }

            List<String> values = dataMap.get(selectionList.get(0));

            targetJList.setListData(values.toArray());
        }

    }
}
